package org.example.business;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AlexaMessageSender {
    private static final String SKILL_ENDPOINT = "https://api.amazonalexa.com/v3/events";

    public static void sendMessage(String accessToken, String eventJson) throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI(SKILL_ENDPOINT))
                .header("Authorization", "Bearer " + accessToken)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(eventJson))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println("Response status code: " + response.statusCode());
        System.out.println("Response body: " + response.body());
    }

    public static void main(String[] args) {
        try {
            String accessToken = AlexaAuth.getAccessToken();

            // 构建AddOrUpdateReport事件JSON
            String eventJson = "{"
                    + "\"event\": {"
                    + "  \"header\": {"
                    + "    \"namespace\": \"Alexa.DeviceDiscovery\","
                    + "    \"name\": \"AddOrUpdateReport\","
                    + "    \"messageId\": \"abc-123-def-456\","
                    + "    \"payloadVersion\": \"3\""
                    + "  },"
                    + "  \"payload\": {"
                    + "    \"endpoints\": ["
                    + "      {"
                    + "        \"endpointId\": \"QS4202400000141\","
                    + "        \"manufacturerName\": \"qamsong\","
                    + "        \"friendlyName\": \"one camera one\","
                    + "        \"description\": \"test\","
                    + "        \"capabilities\": ["
                    + "          {"
                    + "            \"type\": \"AlexaInterface\","
                    + "            \"interface\": \"Alexa\","
                    + "            \"version\": \"3\""
                    + "          },"
                    + "          {"
                    + "            \"type\": \"AlexaInterface\","
                    + "            \"interface\": \"Alexa.PowerController\","
                    + "            \"version\": \"3\","
                    + "            \"properties\": {"
                    + "              \"supported\": ["
                    + "                {"
                    + "                  \"name\": \"powerState\""
                    + "                }"
                    + "              ],"
                    + "              \"proactivelyReported\": true,"
                    + "              \"retrievable\": true"
                    + "            }"
                    + "          }"
                    + "        ],"
                    + "        \"displayCategories\": [\"SWITCH\"],"
                    + "        \"cookie\": {}"
                    + "      }"
                    + "    ]"
                    + "  }"
                    + "}"
                    + "}";

            sendMessage(accessToken, eventJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

